home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / clu / mint.c < prev    next >
C/C++ Source or Header  |  1993-07-25  |  5KB  |  235 lines

  1.  
  2. /* Copyright Massachusetts Institute of Technology 1990,1991 */
  3.  
  4. #ifndef lint
  5. static char rcsid[] = "$Header: mint.c,v 1.3 91/07/09 15:18:13 root Exp $";
  6. #endif lint
  7. /* $Log:    mint.c,v $
  8.  * Revision 1.3  91/07/09  15:18:13  root
  9.  * == -> = (caught by lint)
  10.  * 
  11.  * Revision 1.2  91/06/06  13:55:13  dcurtis
  12.  * added copyright notice
  13.  * 
  14.  * Revision 1.1  91/02/04  23:21:35  mtv
  15.  * Initial revision
  16.  * 
  17.  */
  18.  
  19. /*                        */
  20. /*                        */
  21. /*        IMPLEMENTATION OF        */
  22. /*            mint            */
  23. /*                        */
  24.  
  25. #include "pclu_err.h"
  26. #include "pclu_sys.h"
  27.  
  28. /* MODIFIED BY ROBERT G. FERMIER */
  29.  
  30. #include <sys/types.h>
  31. #include <sys/dir.h>
  32. #include <limits.h>
  33.  
  34. #ifdef sparc
  35. #define WORD_BIT 32
  36. #endif
  37.  
  38. extern CLUREF clu_empty_string;
  39. extern char **environ;
  40. extern int wrpipe;
  41. extern int errno;
  42.  
  43. errcode i_and(i, j, ans)
  44. CLUREF i, j, *ans;
  45. {
  46.     ans->num = i.num & j.num;
  47.     signal(ERR_ok);
  48.     }
  49.  
  50. errcode i_or(i, j, ans)
  51. CLUREF i, j, *ans;
  52. {
  53.     ans->num = i.num | j.num;
  54.     signal(ERR_ok);
  55.     }
  56.  
  57.  
  58. errcode i_xor(i, j, ans)
  59. CLUREF i, j, *ans;
  60. {
  61.     ans->num = i.num ^ j.num;
  62.     signal(ERR_ok);
  63.     }
  64.  
  65. errcode i_not(i, ans)
  66. CLUREF i, *ans;
  67. {
  68.     ans->num = ~i.num;
  69.     signal(ERR_ok);
  70.     }
  71.  
  72. errcode i_shift(i, cnt, ans)
  73. CLUREF i, cnt, *ans;
  74. {
  75. int temp_res;
  76.     /* handle the case where the number to be shifted is zero */
  77.     if (i.num == 0) {
  78.         ans->num = 0;
  79.         signal(ERR_ok);
  80.         }
  81.     /* handler the case where the number doesn't get shifted at all */
  82.     if (cnt.num == 0) {
  83.         ans->num = i.num;
  84.         signal(ERR_ok);
  85.         }
  86.     if (cnt.num > 0) {
  87.         /* we are multiplying by a power of 2 */
  88.         /* |result| should be greater than |i| */
  89.         /* else overflow */
  90.         if (cnt.num >= WORD_BIT) signal(ERR_overflow);
  91.         temp_res = i.num << cnt.num;
  92.                if (i.num > 0 && temp_res <= i.num) signal(ERR_overflow);
  93.                if (i.num < 0 && temp_res >= i.num) signal(ERR_overflow);
  94.         ans->num = temp_res;
  95.         signal(ERR_ok);
  96.         }
  97.     else {
  98.         /* we are dividing by a power of 2 */
  99.         /* |result| should be less than |i| */
  100.         /* else result is 0 for positive input, -1 for negative input */
  101.         if (-cnt.num >= WORD_BIT) {
  102.             if (i.num > 0) temp_res = 0;
  103.             else temp_res = -1;
  104.             }
  105.         else {
  106.             temp_res = i.num >> -cnt.num;
  107.             if (i.num > 0 && temp_res >= i.num) temp_res = 0;
  108.             if (i.num < 0 && temp_res <= i.num) temp_res = -1;
  109.             }
  110.         ans->num = temp_res;
  111.         signal(ERR_ok);
  112.         }
  113.     }
  114.  
  115. errcode i_rotate(input, cnt, ans)
  116. CLUREF input, cnt, *ans;
  117. {
  118. int i,j;
  119. int temp, cnt2;
  120.  
  121. /* Probably a table lookup system would be faster */
  122.  
  123.     cnt2 = cnt.num;
  124.     if (cnt.num < 0) cnt2 = 32 + cnt.num;
  125.     temp = 0;
  126.     for (i = 0; i < 32; i++) {
  127.         if (input.num & 1 << i) {
  128.             j = (i + cnt2) % 32;
  129.             temp |= 1 << j;
  130.             }
  131.         }
  132.     ans->num = temp;
  133.     signal(ERR_ok);
  134.     }
  135.  
  136. errcode i_get (i, bit, cnt, ans)
  137. CLUREF i, bit, cnt, *ans;
  138. {
  139. int temp;
  140. int j,k;
  141.  
  142.     if (cnt.num <= 0) signal(ERR_illegal_size);
  143.     if (bit.num >= 32) signal(ERR_bounds);
  144.     if (cnt.num - bit.num - 1 < 0) signal(ERR_bounds);
  145.     temp = 0;
  146.     for (j = bit.num, k = 0; j <= cnt.num; j++, k++) {
  147.         if (i.num & (1 << (j))) {
  148.             temp |= (1 << k);
  149.             }
  150.         }
  151.     ans->num = temp;
  152.     signal(ERR_ok);
  153.     }
  154.  
  155. errcode i_set (i, bit, cnt, j, ans)
  156. CLUREF i, bit, cnt, j, *ans;
  157. {
  158. int temp;
  159. int j1, k;
  160.  
  161.     if (cnt.num <= 0) signal(ERR_illegal_size);
  162.     if (bit.num >= 32) signal(ERR_bounds);
  163.     if (cnt.num - bit.num - 1 < 0) signal(ERR_bounds);
  164.     temp = i.num;
  165.     for (j1 = bit.num, k = 0; j1 <= cnt.num; j1++, k++) {
  166.         if (j.num & (1 << (j1))) temp |= (1 << k);
  167.         else temp &= ~(1 << k);
  168.         }
  169.     ans->num = temp;
  170.     signal(ERR_ok);
  171.     }
  172.  
  173. /* return the value of the bit'th bit in i */
  174. /* return values are 0 or 1 */
  175. /* min value for bit is 0, max value is 31 */
  176. errcode i_get1(i, bit, ans)
  177. CLUREF i, bit, *ans;
  178. {
  179.     if (bit.num >= 32) signal(ERR_bounds);
  180.     if (i.num & (1 << (bit.num))) ans->num = 1;
  181.     else ans->num = 0;
  182.     signal(ERR_ok);
  183.     }
  184.  
  185. /* change the value of the bit'th bit in i to b */
  186. /* min value for bit is 0, max value is 31 */
  187. errcode i_set1(i, bit, b, ans)
  188. CLUREF i, bit, b, *ans;
  189. {
  190. int temp;
  191.  
  192.     if (bit.num >= 32) signal(ERR_bounds);
  193.     temp = i.num;
  194.     if (b.tf) ans->tf = temp | (1 << (bit.num));
  195.     else ans->tf = temp & ~(1 << (bit.num));
  196.     signal(ERR_ok);
  197.     }
  198.  
  199.  
  200. /* return index of most significant bit that is set */
  201. /* min index = 0, max index = 31 */
  202. errcode i_first1(i, ans)
  203. CLUREF i, *ans;
  204. {
  205. int j;
  206.     if (i.num > 0) {
  207.         for (j = 31; j > -1; j--) {
  208.             if (i.num & (1 << j)) break;
  209.             }
  210.         ans->num = j + 1;
  211.         signal(ERR_ok);
  212.         }
  213.     if (i.num < 0) {
  214.         ans->num = 31;
  215.         }
  216.     signal(ERR_none);
  217.     }
  218.  
  219. /* return index of least significant bit that is set */
  220. /* min index = 0, max index = 31 */
  221. errcode i_last1(i, ans)
  222. CLUREF i, *ans;
  223. {
  224. int j;
  225.  
  226.     if (i.num == 0) signal(ERR_none);
  227.     for (j = 0; j < 32; j++) {
  228.         if (i.num & (1 << j)) break;
  229.         }
  230.     ans->num = j;    /* removed +1 6/4/90 */
  231.     signal(ERR_ok);
  232.     }
  233.  
  234.  
  235.